<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\facade\{View};
use think\exception\ValidateException;

use app\common\model\{User, UserLabel, UserNews, Hall, CollegeTeacher};
use app\common\validate\{HallValidate, TeacherValidate};

class CollegeController extends BaseController
{
    private $userModel;
    public function __construct(User $userModel)
    {
        parent::initialize();
        $this->userModel = $userModel;
    }

    /** 渲染课堂课程列表页面 */
    public function hall_list()
    {
        return view();
    }

    /** 渲染课堂课程添加页面 */
    public function hall_add()
    {
        return view();
    }

    /** 渲染课堂课程编辑页面 */
    public function hall_edit()
    {
        $data = input('');

        try{
            validate(HallValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $hallModel = new Hall();

        $info = $hallModel->getFind(['id' => $data['id']]);

        View::assign('info', $info['data']);
        return view();
    }

    /** 课堂课程数据列表 */
    public function hall_data()
    {
        $data = input('get.');

        try{
            validate(HallValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where = [];

        /** 搜索条件 */
        if($data && isset($data['key'])){
            if(!empty($data['key']['nickname'])){
                $user_ids = $this->userModel->getCol([['nickname', 'like', '%' . $data['key']['nickname'] . '%']], 'id')['data'];
                $where[] = ['user_id', 'in', $user_ids];
            }
        }

        $hallModel = new Hall();

        /** 查询数据总条数 */
        $count = $hallModel->getNum($where)['data'];

        /** 查询列表 */
        $list = $hallModel->getList($where, 1, $data['page'], $data['limit'], '*', 'status, create_time DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        foreach($list['data'] as $k => $v){
            if($v['user_id']){
                $user_info = $this->userModel->getFind(['id' => $v['user_id']], 'nickname');

                if($user_info['code'] != 'ok'){
                    unset($list['data'][$k]);
                    continue;
                }

                $v['nickname'] = $user_info['data']['nickname'];
            }else{
                $v['nickname'] = '-';
            }
        }

        $this->success_back('ok', $list['data'], $count);
    }

    /** 课堂课程添加 */
    public function hall_add_data()
    {
        $data = input('post.');
        
        $data['head_img'] = request()->file('head_img');

        // 验证数据
        try{
            validate(HallValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $hallModel = new Hall();

        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time']   = strtotime($data['end_time']);
        $data['head_img']   = fileUp($data['head_img'], 'hall');
        $flag = $hallModel->add($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('添加成功');
    }

    /** 课堂课程修改 */
    public function hall_edit_data()
    {
        $data = input('post.');
        
        $data['head_img'] = request()->file('head_img');

        if($data['head_img']){
            $field = 'edit_img';
        }else{
            $field = 'edit';
        }

        try{
            validate(HallValidate::class)->scene($field)->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $hallModel = new Hall();

        if($data['head_img']){
            $data['head_img'] = fileUp($data['head_img'], 'hall');
        }else{
            unset($data['head_img']);
        }

        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time']   = strtotime($data['end_time']);

        $flag = $hallModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('编辑成功');
    }

    /** 课堂课程删除 */
    public function hall_del()
    {
        $data = input('post.');

        try{
            validate(HallValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $hallModel = new Hall();

        $flag = $hallModel->del(['id' => $data['id']]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('删除成功');
    }




    /** 渲染学院师资列表页面 */
    public function teacher_list()
    {
        return view();
    }

    /** 渲染学院师资添加页面 */
    public function teacher_add()
    {
        return view();
    }

    /** 渲染学院师资编辑页面 */
    public function teacher_edit()
    {
        $data = input('');

        try{
            validate(TeacherValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $teacherModel = new CollegeTeacher();

        $info = $teacherModel->getFind(['id' => $data['id']]);

        View::assign('info', $info['data']);
        return view();
    }

    /** 学院师资数据列表 */
    public function teacher_data()
    {
        $data = input('get.');

        try{
            validate(TeacherValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $where = [];

        /** 搜索条件 */
        if($data && isset($data['key'])){
            if(!empty($data['key']['name'])){
                $where[] = ['name', 'like', '%' . $data['key']['name'] . '%'];
            }
        }

        $teacherModel = new CollegeTeacher();

        /** 查询数据总条数 */
        $count = $teacherModel->getNum($where)['data'];

        /** 查询列表 */
        $list = $teacherModel->getList($where, 1, $data['page'], $data['limit'], '*', 'id DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        $this->success_back('ok', $list['data'], $count);
    }

    /** 学院师资添加 */
    public function teacher_add_data()
    {
        $data = input('post.');
        
        $data['head_img'] = request()->file('head_img');

        // 验证数据
        try{
            validate(TeacherValidate::class)->scene('add')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $teacherModel = new CollegeTeacher();

        $data['head_img'] = fileUp($data['head_img'], 'teacher');
        $flag = $teacherModel->add($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('添加成功');
    }

    /** 学院师资修改 */
    public function teacher_edit_data()
    {
        $data = input('post.');
        
        $data['head_img'] = request()->file('head_img');

        if($data['head_img']){
            $field = 'edit_img';
        }else{
            $field = 'edit';
        }

        // 验证数据
        try{
            validate(TeacherValidate::class)->scene($field)->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $teacherModel = new CollegeTeacher();

        if($data['head_img']){
            $data['head_img'] = fileUp($data['head_img'], 'teacher');
        }else{
            unset($data['head_img']);
        }

        $flag = $teacherModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('编辑成功');
    }

    /** 学院师资删除 */
    public function teacher_del()
    {
        $data = input('post.');

        try{
            validate(TeacherValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $teacherModel = new CollegeTeacher();

        $flag = $teacherModel->del(['id' => $data['id']]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('删除成功');
    }
}
